package leetcode;



/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 反转链表  1->2->3->4->null   返转 4->3->2->1->null
 * @date 2022/9/7 21:11
 */
public class Code206 {

    public ListNode reverseList(ListNode head) {
        // 当前指针节点
        ListNode current = head;
        // 前指针节点
        ListNode tail = null;
        while (current != null) {
            // 用一个中间变量来当前指针的下一个节点，用于后移
            ListNode mid = current.next;
            // 当前节点的前一个节点
            current.next = tail;
            // 前指针后移
            tail = current;
            // 当前指针后移
            current = mid;

        }
        return tail;

    }



    public static void main(String[] args) {

    }



    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
